/**
 * 本地红点处理类，所有的更新都需要在这里添加 
 */
'use strict';
;(function(){
    var RedDot = function(){
        var ls = window.localStorage;
        this.revKey = "local_rev";
        try{
            this.localRev = JSON.parse(ls.getItem(this.revKey));
        }catch(e){
            this.localRev = undefined;
        }
        if(!this.localRev){
            this.localRev = {};
        }
        this.serverRev = {};
    };
    
    window.RedDot = RedDot;
    var instance; 
    RedDot.getInstance = function(){
       if(!instance){
           instance = new RedDot();
       } 
       return instance;
    };
    
    /**
     * 更新服务器版本
     * return 返回计算后的版本信息数组
     */
    RedDot.prototype.redDotList = function(list){
       var len = list? list.length: 0;
       var arr = [];
       for(var i = 0; i < len; i++){
           var obj1 = list[i];
           this.serverRev[obj1.id] = obj1;
           var revNum = this.localRev[obj1.id];
            var num = 0;
           if(revNum){                
               num = obj1.rev - revNum;
               num = Math.max(0, num);
           }else{
               num = Math.min(99, obj1.rev);
           }
           
           if(num > 0){
               arr.push({id: obj1.id, num: num}) 
           }
       }
       return arr;
    };

    RedDot.prototype.setRev = function(name, rev){
        this.localRev[name] = rev;
        window.localStorage.setItem(this.revKey, JSON.stringify(this.localRev));
    };

})();

if (typeof(module) !== 'undefined')
{
    module.exports = window.RedDot;
}
else if (typeof define === 'function' && define.amd) {
    define([], function () {
        return window.RedDot;
    });
}